/**
 * ReportGear(2011)
 */
package com.reportgear.designer;

import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.HeadlessException;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.SwingUtilities;

import com.reportgear.core.swing.pane.base.JBaseDialog;
import com.reportgear.designer.action.support.JNewReportPane;
import com.reportgear.designer.setting.DesignerContext;
import com.reportgear.designer.ui.toolbar.CommonToolBar;
import com.reportgear.designer.view.ReportListView;
import com.reportgear.designer.view.WorkbenchView;

/**
 * 报表设计器面板
 * 
 * @version 1.0 2011-3-14
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since Report 1.0
 * 
 */
public class DesignerPane extends JPanel {
	/**
	 * UID
	 */
	private static final long serialVersionUID = 2275159869301485499L;
	private ReportListView reportList;
	private WorkbenchView workbench;
	private JSplitPane mainPane;

	public DesignerPane() throws HeadlessException {
		this.initComponents();
		this.initDesigner();
	}

	/**
	 * 初始化控件
	 */
	private void initComponents() {
		this.setLayout(new BorderLayout());
		JToolBar styleBar = new CommonToolBar();
		this.add(styleBar, BorderLayout.NORTH);

		this.reportList = new ReportListView();
		this.workbench = new WorkbenchView();

		this.reportList.addPropertyChangeListener("currReport", this.workbench);
		this.reportList.selectFirst();

		mainPane = this.createSplitPane(this.reportList, new JScrollPane(this.workbench));

		this.add(this.mainPane, BorderLayout.CENTER);
	}

	/**
	 * 建立分隔面板
	 * 
	 * @param left
	 *            左面板
	 * @param right
	 *            右面板
	 * @return 分隔面板
	 */
	private JSplitPane createSplitPane(Component left, Component right) {
		JSplitPane pane = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, left, right);
		pane.setOneTouchExpandable(true);
		pane.setDividerLocation(200);

		return pane;
	}

	/**
	 * 初始化编辑器
	 */
	private void initDesigner() {
		DesignerContext.INSTANCE.setDesigner(this);
	}

	/**
	 * 创建新报表
	 */
	public void newReport() {
		JNewReportPane pane = new JNewReportPane();
		int result = pane.showDialog(SwingUtilities.getWindowAncestor(this), "新建薪酬报表", 400, 180);

		if (result == JBaseDialog.RESULT_OK) {
			if (pane.isInputValid()) {
				this.reportList.newReport(pane.getName(), pane.getSchemeId());
			} else {
				JOptionPane.showMessageDialog(this, "输入信息不完整!");
			}
		}

	}

	/**
	 * 保存报表
	 */
	public void saveReport() {
		this.workbench.saveReport();
	}

	/**
	 * 删除报表
	 */
	public void deleteReport() {
		if (JOptionPane.showConfirmDialog(this, "请确认删除报表模板", "删除", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
			this.reportList.deleteReport();
		}
	}
}
